package gui;

import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JButton;

import model.Guest;
import ctr.Gctr;
import ctr.RRctr;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JLabel;
import javax.swing.JTextField;

public class CreateReservation extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2476058436914082377L;
	private RRctr ctr = new RRctr();
	private JList lstGuests;
	private Guest guest;
	private JTextField statusField;
	private JTextField inField;
	private JTextField outField;

	/**
	 * Create the panel.
	 */
	public CreateReservation() {
		setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(273, 0, 177, 229);
		add(scrollPane);
		
		 lstGuests = new JList();
		scrollPane.setViewportView(lstGuests);
		
		JButton btnShowGuest = new JButton("Show Guests");
		btnShowGuest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateGuestList();
				
			}
		});
		btnShowGuest.setBounds(321, 237, 117, 25);
		add(btnShowGuest);
		
		JButton btnSelectGuest = new JButton("Select Guest");
		btnSelectGuest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				choseGuest();
				CreateRoomReservation();
			}
		});
		btnSelectGuest.setBounds(321, 262, 117, 25);
		add(btnSelectGuest);
		
		JLabel lblNewLabel = new JLabel("Status");
		lblNewLabel.setBounds(12, 20, 56, 16);
		add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("CheckIn");
		lblNewLabel_1.setBounds(12, 60, 56, 16);
		add(lblNewLabel_1);
		
		JLabel lblCheckout = new JLabel("CheckOut");
		lblCheckout.setBounds(12, 100, 56, 16);
		add(lblCheckout);
		
		statusField = new JTextField();
		statusField.setBounds(119, 17, 116, 22);
		add(statusField);
		statusField.setColumns(10);
		
		inField = new JTextField();
		inField.setBounds(119, 57, 116, 22);
		add(inField);
		inField.setColumns(10);
		
		outField = new JTextField();
		outField.setBounds(119, 97, 116, 22);
		add(outField);
		outField.setColumns(10);

	}
	
	private void updateGuestList()
	{
		ArrayList<Guest> list = new Gctr().findAllGuests();
		DefaultListModel<Guest> dlist = new DefaultListModel<>();
		for(Guest guest: list)
			dlist.addElement(guest);
		lstGuests.setModel(dlist);
	}
	
	public void choseGuest() {
		
		this.guest = (Guest)this.lstGuests.getSelectedValue();
	}

	public Guest getGuest() {
		return guest;
	}

	public void setGuest(Guest guest) {
		this.guest = guest;
	}
	
	public void CreateRoomReservation()
	{
		String status = statusField.getText();
		String checkIn = inField.getText();
		String checkOut = outField.getText();
		int id = guest.getId();
		ctr.insertNew(status, checkIn, checkOut, id);
		
	}

}
